<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <script>
    Promise.myAll = function (promise) {
      if (!promise || typeof promise[Symbol.iterator] !== 'function') {
        throw new Error('Arg must be an iterator');
      }
      return new Promise((resolve, reject) => {
        const promisesArray = Array.from(promise);
        if (promisesArray.length === 0) {
          resolve([]);
          return;
        }

        const results = new Array(promisesArray.length);
        let completeCount = 0;

        promisesArray.forEach((item, i) => {
          Promise.resolve(item)
            .then((value) => {
              results[i] = value;
              completeCount++;

              if (completeCount === promisesArray.length) {
                resolve(results);
              }
            })
            .catch(reject);
        });
      });
    };
    const promise1 = Promise.resolve(1);
    const promise2 = new Promise((resolve) =>
      setTimeout(() => resolve(2), 1000)
    );
    const promise3 = 3; // 普通值
    Promise.myAll([promise1, promise2, promise3])
      .then((values) => console.log('成功:', values)) // 输出: [1, 2, 3]
      .catch((error) => console.error('失败:', error));
  </script>
  <body></body>
</html>
